package com.cqq.pro.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.fileupload.FileItem;

/**
 *
 * @author CHENQQ
 */
public class DownOrUpLoadUtil {
    
    /**
     * 保存上传文件
     * @param saveFilePath 存储路径
     * @param fileName 文件名
     * @param item 文件写入流
     */
    public static void saveFile(String saveFilePath,String fileName,FileItem item){
        File f1 = new File(saveFilePath);
        if (!f1.exists()) {
            f1.mkdirs();
        }
        File saveFile = new File(saveFilePath+fileName);
        try {
            item.getString("UTF-8");
            item.write(saveFile);
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    /**
     * 固定字节数大小写入文件
     * @param inputStream
     * @param saveFilePath
     * @param fileName
     * @throws IOException
     */
    public static void saveBigFile(InputStream inputStream,String saveFilePath,String fileName) throws IOException{
		File f1 = new File(saveFilePath);
	    if (!f1.exists())
	        f1.mkdirs();
	    
		FileOutputStream fos = new FileOutputStream(saveFilePath+fileName);
		BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
		int length = 0;
		byte[] buffer = new byte[1024];
		while ((length = inputStream.read(buffer)) != -1) {
		    bos.write(buffer, 0, length);
		}
		bos.flush();
		bos.close();
		inputStream.close();
	}

    /**
     * 获取当前文件下的所有文件
     * @param pathOfFolder
     * @return 
     */
    public static File[] getFilesByNameOfFolder(String pathOfFolder) {
        File file = new File(pathOfFolder);
        File[] files = null;
        if(file.exists()){
            if(file.isDirectory()){
                files = file.listFiles();
            }
        }
        return files;
    }
    
    /**
     * 根据文件路径集合，获取文件对象集合
     * @param urlOfImag
     * @return 
     */
    public static File[] getFilesByNameOfFolder(String[] urlOfImag) {
        if(urlOfImag.length<=0)return null;
        List<File> fileList = new ArrayList<File>();
        for(int i=0;i<urlOfImag.length;i++){
             File file = new File(urlOfImag[i]);
             if(file.exists()){
                if(file.isFile()){
                    fileList.add(file);
                }
             }
        }
        
        File[] files = new File[fileList.size()];
        for(int i=0;i<fileList.size();i++){
            files[i] = fileList.get(i);
        }
        return files;
    }
    /**
     * 根据文件路径集合，获取文件对象集合
     * @param urlOfImag
     * @return 
     */
     public static File[] getFilesByNameOfFolder(List<String> urlOfImag) {
        if(urlOfImag.size()<=0)return null;
        List<File> fileList = new ArrayList<File>();
        for(int i=0;i<urlOfImag.size();i++){
             File file = new File(urlOfImag.get(i));
             if(file.exists()){
                if(file.isFile()){
                    fileList.add(file);
                }
             }
        }
        
        File[] files = new File[fileList.size()];
        for(int i=0;i<fileList.size();i++){
            files[i] = fileList.get(i);
        }
        return files;
    }
     
     /**
      * 删除文件
      * @param filePath
      */
	 public static void deleteFile(String filePath){
		File file = new File(filePath);
	    if (file.isFile() && file.exists()) {  
	        file.delete();  
	    }
	}
	 
}
